package hasthi.tools.analyze;

import static hasthi.tools.analyze.AnalyzeUtils.confidence95Interval;
import static hasthi.tools.analyze.AnalyzeUtils.format;
import static hasthi.tools.analyze.AnalyzeUtils.formatOne;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;

import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;

public class Experiment {
    private static DecimalFormat df = new DecimalFormat(".0");
    private Map<String, DescriptiveStatistics> dataMap = new TreeMap<String, DescriptiveStatistics>();

    public void addDatapoint(String key, double val) {
        DescriptiveStatistics descriptiveStatistics = dataMap.get(key);
        if (descriptiveStatistics == null) {
            descriptiveStatistics = DescriptiveStatistics.newInstance();
            dataMap.put(key, descriptiveStatistics);
        }
        descriptiveStatistics.addValue(val);
    }
    
    
    public DescriptiveStatistics getData(String key){
        DescriptiveStatistics descriptiveStatistics = dataMap.get(key);
        if (descriptiveStatistics == null) {
            descriptiveStatistics = DescriptiveStatistics.newInstance();
            dataMap.put(key, descriptiveStatistics);
        }
        return descriptiveStatistics; 
    }
    

    public String printdetails(String name) {
        StringBuffer buf = new StringBuffer();
        buf.append("### ").append(name).append(" ####\n");
        
        FormattedTable table = new FormattedTable("Name\t\t\tmean\tmedian\tcov\tsize\tmin\tmax\tconfi");
        for (String datasetName : dataMap.keySet()) {
            table.addLine(format(datasetName) + "\t"+ formatOne(dataMap.get(datasetName)));
        }
        buf.append(table.toString());
        return buf.toString();
    }

    public String toString() {
        return set1();
    }

    public void dataPoint(StringBuffer buf, DescriptiveStatistics data) {
        StringBuffer buft = new StringBuffer();
        buft.append(df.format(data.getMean())).append("(").append(
                df.format(confidence95Interval(data))).append(")");
        buf.append(AnalyzeUtils.format(buft.toString()));
    }

    public String set1() {
        StringBuffer buf = new StringBuffer();
        for (String datasetName : dataMap.keySet()) {
            DescriptiveStatistics ds = dataMap.get(datasetName);
            dataPoint(buf, ds);
            // buf.append(df.format(ds.getPercentile(0.5))).append("\t");
        }
        // buf.append("|");
        // for (String datasetName : dataMap.keySet()) {
        // DescriptiveStatistics ds = dataMap.get(datasetName);
        // buf.append(df.format(confidence95Interval(ds))).append("\t");
        // }
        return buf.toString();
    }

    public String getHeader() {
        StringBuffer buf = new StringBuffer();
        buf.append(AnalyzeUtils.format("Key") + "\t");
        for (String datasetName : dataMap.keySet()) {
            // buf.append(AnalyzeUtils.format(datasetName)).append("\t");
            buf.append(AnalyzeUtils.format(datasetName)+"\t");
        }
        return buf.toString();
    }

    public void writeRowData(File file) throws IOException {
        FileWriter writer = new FileWriter(file);
        BufferedWriter bw = new BufferedWriter(writer);

        double[][] rawdata = new double[dataMap.size()][];
        int i = 0;
        StringBuffer buf = new StringBuffer();
        buf.append("#");
        for (String datasetName : dataMap.keySet()) {
            buf.append(datasetName);
            buf.append("\t");
            rawdata[i] = dataMap.get(datasetName).getValues();
            i++;
        }
        
        

        double[][] transposedData = AnalyzeUtils.transpose(rawdata);
//        
//        double[][] truncatedData = new double[datacount][];
//        System.arraycopy(transposedData, 0, truncatedData, 0, datacount);
        
        String data = AnalyzeUtils.array2str(transposedData, buf.toString());
       bw.write(data);
        bw.flush();
        writer.close();
    }
    
    
    public void processData(){
        DescriptiveStatistics data = dataMap.get("CLE2E");
        if(data != null){
            double[] rawdata = data.getValues();
            
            DescriptiveStatistics d1 = DescriptiveStatistics.newInstance();
            DescriptiveStatistics d2 = DescriptiveStatistics.newInstance();
            
            int cutoff = 15;
            
            cutoff = Math.round(data.getValues().length*0.15f);
            
            for(int i = 0;i<rawdata.length;i++){
                double val = rawdata[i];
                if(i < cutoff){
                    d1.addValue(val);
                }else{
                    d2.addValue(val);
                }
            }
            dataMap.put("CLE2E",d2 );
            dataMap.put("CLE2EHead",d1 );
        }
    }
    
}